package com.hou;

import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Flow;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class Demo02 {
    @Test
    public void test01(){
        String str01="  \n hou  zheng  \r\n  ";
        String str02="   \t   \r\n   ";
        str02.isBlank();// 判断字符串是否为空白,包括空格换行符等都是空白
        "".isEmpty();//只会判断空字符串
        /**
         * trim与strip区别,trim只能取消码值小于等于32的,即英文的空格,汉字的无法去除
         * 而strip更强大,可以去除任何语言的空白字符
         */
        str01.strip();//去重字符串首尾空白
        str01.stripLeading();//去重首部空白
        str01.stripTrailing();//去重尾部空白
        String str03="ha";
        //复制字符串,0返回n空字符串""
        System.out.println(str03.repeat(2));
        System.out.println(str03.repeat(0).equals(""));//true
        String str04="hou \n zheng \n";
        //lines:返回Stream流,可求行数
        System.out.println(str04.lines().count());
    }

    @Test
    public void test02(){
        var optional = Optional.ofNullable(null);
        //新提供:
        optional.orElseThrow();//为空抛异常
        //不为bull消费数据,存在执行另一个线程
        optional.ifPresentOrElse(x-> System.out.println("数据:"+x),()-> System.out.println("数据不存在"));
    }

    @Test
    public void test03() throws Exception {
        var httpClient = HttpClient.newHttpClient();
        //同步调用接口
        var httpResponse = httpClient.send(
                HttpRequest.newBuilder(URI.create("http://localhost:8080/name")).GET().build(),
                HttpResponse.BodyHandlers.ofString());
        httpResponse.body();
        httpResponse.statusCode();
        //异步发送消息,立即返回结果Future
        CompletableFuture<HttpResponse<String>> httpResponseCompletableFuture =
                httpClient.sendAsync(HttpRequest.newBuilder(URI.create("")).
                                POST(HttpRequest.BodyPublishers.ofString("参数")).
                                setHeader("","").build(),
                HttpResponse.BodyHandlers.ofString());
        httpResponseCompletableFuture.get();//阻塞获取
    }
    @Test
    public void test06(){
        var list = new ArrayList<String>();
        for (int i = 0; i < 5; i++) {list.add(String.valueOf(i));}
        list.stream().filter(value-> Pattern.matches("\\d",value)).forEach(System.out::println);
    }

    @Test
    public void test07(){
        var list1 = new ArrayList<String>();
        var list2 = new ArrayList<String>();
        var list3 = new ArrayList<String>();
        for (int i = 0; i < 5; i++) {
            list1.add(String.valueOf(i));
            list2.add(String.valueOf(i));
            list3.add(String.valueOf(i));
        }
        //Stream.of(list1).filter(value-> Pattern.matches("\\d",value)).forEach(System.out::println);
    }
}
